// GTK+ common functions
// Copyright 2006 (C) Ralph Thomas

#include <view/gtk/common.h>

namespace view {
	void scale_aspect( long& x, long& y, long& w1, long& h1, long w2, long h2 ) {
		x = 0;
		y = 0;
		//
		// Preserve the image's aspect ratio.
		//
		int imw( w1 );
		int imh( h1 );
		//
		// Figure out which our smallest normalized dimension is.
		//
		double dw( (double)w1 / (double)w2 );
		double dh( (double)h1 / (double)h2 );

		if( dw > dh ) {
			long rh( h2 );
			h1 = (long)((double)w2 *
				((double)imh / (double)imw));
			w1 = w2;
			y =  y + (rh - h1) / 2;
		} else {
			long rw( w2 );
			w1 = (long)((double)h2 *
				((double)imw / (double)imh));
			h1 = h2;
			x = x + (rw - w1) / 2;
		}
	}
};

